﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace THO7_Team8
{
    class InvertAlgorithm : VisionAlgorithm
    {
        static int BLUE = 0, GREEN = 1, RED = 2, ALPHA = 3;
        public InvertAlgorithm(String name) : base(name) { }
        public override System.Drawing.Bitmap DoAlgorithm(System.Drawing.Bitmap sourceImage)
        {
            Bitmap returnImage = new Bitmap(sourceImage);
            Rectangle rectangle = new Rectangle(0, 0, returnImage.Width, returnImage.Height);
            BitmapData bitmapData =
                returnImage.LockBits(rectangle, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                returnImage.PixelFormat);

            IntPtr ptr = bitmapData.Scan0;

            int size = Math.Abs(bitmapData.Stride) * sourceImage.Height;
            byte[] rgbValues = new byte[size];

            Marshal.Copy(ptr, rgbValues, 0, size);

            int currentColor = BLUE;
            /*for (int i = 0; i < rgbValues.Length - 4; i = i + 4)
            {
                byte blackByte = Convert.ToByte(0);
                rgbValues[i] = Convert.ToByte(255 - Convert.ToInt32(rgbValues[i]));
                rgbValues[i + 1] = Convert.ToByte(255 - Convert.ToInt32(rgbValues[i + 1]));
                rgbValues[i + 2] = Convert.ToByte(255 - Convert.ToInt32(rgbValues[i + 2]));
            }*/
            for (int i = 0; i < rgbValues.Length; i++)
                //rgbValues[i] = Convert.ToByte(255 - rgbValues[i]);
                if (currentColor != ALPHA)
                {
                    rgbValues[i] = Convert.ToByte(255 - Convert.ToInt32(rgbValues[i]));
                    currentColor++;
                }
                else
                {
                    currentColor = BLUE;
                }

            Marshal.Copy(rgbValues, 0, ptr, size);
            returnImage.UnlockBits(bitmapData);
            return returnImage;
        }
    }
}
